import java.util.LinkedList;
import java.util.Queue;

//用队列模拟栈 先进的后出
class MyStack {
    public Queue<Integer> que1;
    public Queue<Integer> que2;

    public MyStack() {
        que1 = new LinkedList<>();
        que2 = new LinkedList<>();
    }

    public void push(int x) {
        if (empty()) {
            que1.offer(x);
            return;
        }
        //放在不为空的队列
        if (!que1.isEmpty()) {
            que1.offer(x);
        } else {
            que2.offer(x);
        }
    }

    public int pop() {
        if (empty()){
            return -1;
        }
            if (!que1.isEmpty()) {
                int size=que1.size();
                for (int i = 0; i < size - 1; i++) {
                    que2.offer(que1.poll());
                }
                return que1.poll();
            } else {
                int size=que2.size();
                for (int i = 0; i < size - 1; i++) {
                    que1.offer(que2.poll());
                }
                return que2.poll();
            }
    }

    public int top () {
        if (empty()){
            return -1;
        }
        if (!que1.isEmpty()) {
            int size=que1.size();
            int val=0;
            for (int i = 0; i < size; i++) {
                val=que1.poll();
                que2.offer(val);
            }
            return val;
        } else {
            int size=que2.size();
            int val=0;
            for (int i = 0; i < size; i++) {
                val=que2.poll();
                que1.offer(val);
            }
            return val;
        }
    }

    public boolean empty () {
        return que1.isEmpty() && que2.isEmpty();
    }

}